package com.example.administrator.demod;

import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class MainActivity extends AppCompatActivity {
    private TextView androidAverage;
    private TextView englishAverage;
    private TextView totalAverage;
    private TextView androidMax;
    private TextView englishMax;
    private TextView totalMax;
    private TextView androidMin;
    private TextView englishMin;
    private TextView totalMin;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Score> scoreArrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        readFromDB();
        readAverage();
        readAndroid();
        readEnglish();
        readTotal();
    }

    //总分最高分和最低分
    private void readTotal() {
        readFromDB();

        //排序
        Collections.sort(scoreArrayList, new SortByTotal());
        for (Student student: studentArrayList){
            if(student.getStuNo().equals(scoreArrayList.get(0).getStuNo())){
                String name = student.getName();
                Score score =  scoreArrayList.get(0);
                int total = score.getEnglish()+score.getEnglish();
                totalMin.setText("总分最低分" + String.valueOf(total)+"      " + name);
            }

            if(student.getStuNo().equals(scoreArrayList.get(scoreArrayList.size()-1).getStuNo())){
                String name = student.getName();
                Score score =  scoreArrayList.get(scoreArrayList.size() - 1);
                int total = score.getEnglish()+score.getEnglish();
                totalMax.setText("总分最高分" + String.valueOf(total)+"      " + name);
            }
        }
    }

    private class SortByTotal implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            Score score1 = (Score) o1;
            Score score2 = (Score) o2;
            if(score1.getEnglish()+score1.getAndroid() > score2.getEnglish()+score2.getAndroid()){
                return 1;
            }
            return -1;
        }
    }

    //英语最高分和最低分
    private void readEnglish() {
        readFromDB();

        //排序
        Collections.sort(scoreArrayList, new SortByEnglish());
        for (Student student: studentArrayList){
            if(student.getStuNo().equals(scoreArrayList.get(0).getStuNo())){
                String name1 = student.getName();
                englishMin.setText("英语最低分" + String.valueOf(scoreArrayList.get(0).getEnglish())
                        +"      " + name1);
            }
            if(student.getStuNo().equals(scoreArrayList.get(scoreArrayList.size()-1).getStuNo())){
                String name2 = student.getName();
                englishMax.setText("英语最高分" + String.valueOf(scoreArrayList.get(scoreArrayList.size() - 1).getEnglish())
                        +"      " + name2);
            }
        }
    }

    //按英语分数升序
    private class SortByEnglish implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            Score score1 = (Score) o1;
            Score score2 = (Score) o2;
            if(score1.getEnglish() > score2.getEnglish()){
                return 1;
            }
            return -1;
        }
    }

    //安卓最高分和最低分
    private void readAndroid() {
        readFromDB();

        //排序
        Collections.sort(scoreArrayList, new SortByAndroid());
        for (Student student: studentArrayList){
            if(student.getStuNo().equals(scoreArrayList.get(0).getStuNo())){
                String name1 = student.getName();
                androidMin.setText("安卓最低分" + String.valueOf(scoreArrayList.get(0).getAndroid())
                        +"      " + name1);
            }
            if(student.getStuNo().equals(scoreArrayList.get(scoreArrayList.size()-1).getStuNo())){
                String name2 = student.getName();
                androidMax.setText("安卓最高分" + String.valueOf(scoreArrayList.get(scoreArrayList.size() - 1).getAndroid())
                        +"      " + name2);
            }
        }

        for (Score score: scoreArrayList) {
            //测试
            Log.e("scoreArrayList", "score.getAndroid() = "+ score.getAndroid() );
        }
    }

    //按安卓分数升序
    private class SortByAndroid implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            Score score1 = (Score) o1;
            Score score2 = (Score) o2;
            if(score1.getAndroid() > score2.getAndroid()){
                return 1;
            }
            return -1;
        }
    }

    //读取各个的平均分
    private void readAverage() {
        readFromDB();

        double result1 = 0;
        double result2 = 0;
        for(int i=0; i<scoreArrayList.size(); i++){
            int android = scoreArrayList.get(i).getAndroid();
            int english = scoreArrayList.get(i).getEnglish();

            result1 += android;
            result2 += english;
        }

        androidAverage.setText("安卓平均分"+String.valueOf(result1/scoreArrayList.size()));
        englishAverage.setText("英语平均分"+String.valueOf(result2/scoreArrayList.size()));
        totalAverage.setText("总分平均分"+String.valueOf((result1+result2)/scoreArrayList.size()));
    }

    private void readFromDB() {
        Uri uri1 = Uri.parse("content://com.hjn.xn/score");
        Cursor cursor1 = getContentResolver().query(uri1,null,null,
                null,null);

        scoreArrayList.clear();
        if(cursor1 != null && cursor1.getCount() > 0){
            for(cursor1.moveToFirst(); !cursor1.isAfterLast(); cursor1.moveToNext()){
                String no = cursor1.getString(0);
                int android = cursor1.getInt(1);
                int english = cursor1.getInt(2);

                Score score = new Score(no, android, english);
                scoreArrayList.add(score);
            }

            cursor1.close();
        }

        Uri uri2 = Uri.parse("content://com.hjn.xn/student");
        Cursor cursor2 = getContentResolver().query(uri2,null,null,
                null,null);

        studentArrayList.clear();
        if(cursor2 != null && cursor2.getCount() > 0){
            for(cursor2.moveToFirst(); !cursor2.isAfterLast(); cursor2.moveToNext()){
                String no = cursor2.getString(0);
                String name = cursor2.getString(1);
                int age = cursor2.getInt(2);
                String sex = cursor2.getString(3);

                Student student = new Student(no, name, age, sex);
                studentArrayList.add(student);
            }
            cursor2.close();
        }
    }

    private void findViews() {
        androidAverage = findViewById(R.id.android_average);
        androidMax = findViewById(R.id.android_max);
        androidMin = findViewById(R.id.android_min);
        englishAverage = findViewById(R.id.english_average);
        englishMax = findViewById(R.id.english_max);
        englishMin = findViewById(R.id.english_min);
        totalAverage = findViewById(R.id.total_average);
        totalMax = findViewById(R.id.total_max);
        totalMin = findViewById(R.id.total_min);
    }
}
